package com.ymttest.business.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.ymatou.iapi.coupon.parameter.BuyerInfo;
import com.ymatou.iapi.coupon.parameter.ConversionCouponBean;
import com.ymatou.iapi.coupon.parameter.OrderInfo;
import com.ymatou.iapi.coupon.parameter.ProductInfo;
import com.ymatou.iapi.coupon.parameter.SellerOrderInfo;
import com.ymatou.iapi.coupon.parameter.ShoppingInfo;
import com.ymatou.iapi.coupon.parameter.UseCouponBean;
import com.ymatou.iapi.coupon.parameter.UseCouponCallbackBean;
import com.ymatou.iapi.coupon.parameter.UseCouponInfo;
import com.ymatou.iapi.coupon.parameter.UserReceiveCouponBean;
import com.ymatou.iapi.coupon.parameter.invalidCouponBean;
import com.ymatou.iapi.coupon.service.ConversionCouponCall;
import com.ymatou.iapi.coupon.service.UseCouponCall;
import com.ymatou.iapi.coupon.service.UseCouponCallbackCall;
import com.ymatou.iapi.coupon.service.UserReceiveCouponCall;
import com.ymatou.iapi.coupon.service.invalidCouponCall;
import com.ymatou.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymatou.promotion.api.parameter.CouponBatchBean;
import com.ymatou.promotion.api.parameter.CreateSellerCouponBean;
import com.ymatou.promotion.api.service.CreateSellerCouponCall;
import com.ymt.core.tool.Logger;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

public class CouponServiceCallServiceV2 {
	
    /**
     * 用户领取优惠券
     * use ymtrelease;
select * from Ymt_CouponBatch where sBatchCode='1706027394'
select * from Ymt_CouponSetting where iCouponSettingId=1472087

update Ymt_CouponBatch set iCouponTotalNum=2000000 where sBatchCode='1706027394'
delete from Ymt_CouponSendIdempotent 
     * 
     * 需要提前创建优惠券
     * 
     * @param batchCode
     * @param userId
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public String receiveCouponInfo(String batchCode, int userId) throws ClientProtocolException, IOException
    {
        Logger.comment("用户" + userId + "领取优惠券，批次为" + batchCode);
        //new YmtCouponSellerPrefixWapper().updateIsOpen(batchCode);
        
        UserReceiveCouponBean userreceivecouponBean = new UserReceiveCouponBean();
    	UserReceiveCouponCall userreceivecouponCall = new UserReceiveCouponCall();
    	
    	userreceivecouponBean.setBatchCode(batchCode);
    	userreceivecouponBean.setUserId(userId);

    	userreceivecouponCall.setData(false, userreceivecouponBean);
    	userreceivecouponCall.callService();

        return userreceivecouponCall.getUserReceiveResult().getCouponCode();
    }

    /**
     * 领用优惠券<br>
     * 创建总数为一张的 每人只能使用一次的 优惠券
     * 
     * @author chenjiazhu
     * 
     * @param sellerId 商家Id
     * @param buyerId 买家Id
     * @param price 优惠券金额
     * @return BatchCode 优惠券号
     * @throws IOException
     * @throws ClientProtocolException
     */
    public String receiveCouponInfo(int sellerId, int buyerId, BigDecimal price) throws ClientProtocolException,
            IOException {
        Logger.comment("用户" + buyerId + "领取商家" + sellerId + "优惠券，券面值" + price);
        // 创建优惠券
		CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
        
        SellerCreateBatchBean sellerCreateBatchBean = CouponServiceMCallServiceV2.setDefaultSellerCreateBatchBean();
       
        sellerCreateBatchBean.setSellerId(sellerId);
        sellerCreateBatchBean.setCouponAmount(price);
        
        SellerCreateBatchCall sellerCreateBatchCall = CouponServiceMCallServiceV2.SellerCreateBatch(sellerCreateBatchBean);

        return receiveCouponInfo(sellerCreateBatchCall.getBatchCode(),buyerId);
    }

    public static invalidCouponCall inValidCoupon(int userId,String batchCode) throws ClientProtocolException, IOException
    {
        Logger.comment("作废优惠券batchCode:" + batchCode);

        invalidCouponBean invalidcouponBean = new invalidCouponBean();
        invalidCouponCall invalidcouponCall = new invalidCouponCall();

        invalidcouponBean.setUserId(userId);
		invalidcouponBean.setBatchCode(batchCode);
		invalidcouponBean.setInvalidMemo("自动化作废优惠券测试");
		invalidcouponBean.setAppId("m.ymatou.com");
		invalidcouponBean.setRequestId("123deddedew2323443tr34");

        invalidcouponCall.setData(false, invalidcouponBean);
        invalidcouponCall.callService();

        return invalidcouponCall;
    }
    
    public static int useCoupon(int userId, String couponCode,Integer orderid)
    {
        return new YmtCouponWapper().useCoupon(userId, couponCode,orderid);
    }    
    
    /**
     * 用券，对订单不做验证
     * 
     * @param userId
     * @param orderId
     * @param couponCode
     * @return
     */
    public UseCouponBean setUseCouponBean(Integer userId, Integer orderId, String couponCode)
    {
    	UseCouponBean usecouponBean = new UseCouponBean();
    	
//    	UseCouponInfo couponInfo = new UseCouponInfo();
//		couponInfo.setOrderId(orderId);
//		couponInfo.setCouponCode(couponCode);
			
		
//		List<UseCouponInfo> useCouponInfos = new ArrayList<>();
//		useCouponInfos.add(couponInfo);
		
		List<String> CouponCodes=new ArrayList();
		CouponCodes.add(couponCode);
		
		usecouponBean.setUserId(userId);
		usecouponBean.setMainOrderId(orderId);
		usecouponBean.setCouponCodes(CouponCodes);
		//usecouponBean.setUseCouponInfos(useCouponInfos);
		usecouponBean.setAppId("mansit");
		usecouponBean.setRequestId("12344566");

		return usecouponBean;
    }
    
    public UseCouponCall useCoupon(UseCouponBean useCouponBean) throws ClientProtocolException, IOException
    {
    	UseCouponCall useCouponCall = new UseCouponCall();
    	
    	useCouponCall.setData(false,useCouponBean);
    	useCouponCall.callService();
    	
    	return useCouponCall;
    }
    
    
    public UseCouponCallbackBean setUseCouponCallbackBean(Integer orderId, String sellerCouponCode, String ymtCouponCode)
    {
    	UseCouponCallbackBean bean = new UseCouponCallbackBean();
    	
    	bean.setMainOrderId(orderId);
    	bean.setSellerCouponCode(sellerCouponCode);
    	bean.setYmtCouponCode(ymtCouponCode);
    	
    	return bean;
    }
    
    public UseCouponCallbackCall UseCouponCallback(UseCouponCallbackBean bean) throws ClientProtocolException, IOException
    {
    	UseCouponCallbackCall call = new UseCouponCallbackCall();
    	call.setData(false,bean);
    	call.callService();
    	
    	return call;
    }
    
    public static ShoppingInfo setDefaultShoppingInfo(int buyerId, String platformCouponCode, String sellerCouponCode)
    {
        Logger.comment("设置默认的购物车");
        ShoppingInfo shoppingInfo = new ShoppingInfo();

        BuyerInfo buyerInfo = new BuyerInfo();

        buyerInfo.setBuyerId(buyerId);
        // 默认下单数0
        buyerInfo.setBuyerOrderNum(0);

        shoppingInfo.setBuyerInfo(buyerInfo);

        SellerOrderInfo sellerOrderInfo = new SellerOrderInfo();

        // 卖家Id
        int sellerId = Integer.parseInt(EnvSetup.getData("jCouponSellerId"));
        sellerOrderInfo.setSellerId(sellerId);

        if (sellerCouponCode != null && sellerCouponCode != "")
        {
            sellerOrderInfo.setCouponCode(sellerCouponCode);
        }

       OrderInfo orderInfo = new OrderInfo();
       ProductInfo productInfo = new ProductInfo();

        String productCode = TestDataManager.getRandomNum(36);
        productInfo.setProductCode(productCode);
        productInfo.setActivityId(0);
        productInfo.setBrandId(2111);
        productInfo.setCategoryId(3111);
        productInfo.setProductQuantity(1);
        productInfo.setProductPrice(new BigDecimal(1000));

        List<ProductInfo> productInfos =
                new ArrayList<ProductInfo>();
        productInfos.add(productInfo);
        orderInfo.setProductInfos(productInfos);

        List<OrderInfo> orderInfos = new ArrayList<OrderInfo>();
        orderInfos.add(orderInfo);

        sellerOrderInfo.setOrderInfos(orderInfos);

        List<SellerOrderInfo> sellerOrderInfos = new ArrayList<SellerOrderInfo>();
        sellerOrderInfos.add(sellerOrderInfo);

        shoppingInfo.setSellerOrderInfos(sellerOrderInfos);

        if (platformCouponCode != null && platformCouponCode != "")
        {
            shoppingInfo.setPlatformCouponCode(platformCouponCode);
        }

        shoppingInfo.setPlatformUsage(1);

        return shoppingInfo;
    }
    
    public static ShoppingInfo setDefaultShoppingInfo(int buyerId, Integer sellerId, String platformCouponCode, String sellerCouponCode)
    {
        Logger.comment("设置默认的购物车");
        ShoppingInfo shoppingInfo = setDefaultShoppingInfo(buyerId,platformCouponCode,sellerCouponCode);
        shoppingInfo.getSellerOrderInfos().get(0).setSellerId(sellerId);
        return shoppingInfo;
    }
    public ConversionCouponBean setConversionCouponBean(int userId, String conversioncode)
    {
    	ConversionCouponBean  conversioncouponBean = new ConversionCouponBean();
    	
    	conversioncouponBean.setUserId(userId);
		conversioncouponBean.setConversionCode(conversioncode);
		conversioncouponBean.setAppId("Ymt");
		conversioncouponBean.setRequestId("12344");
		conversioncouponBean.setUserType(0);

		return conversioncouponBean;
    }
    
    public ConversionCouponCall ConversionCoupon(ConversionCouponBean bean) throws ClientProtocolException, IOException
    {
    	ConversionCouponCall call = new ConversionCouponCall();
    	call.setData(false,bean);
    	
    	call.callService();
    	
    	return call;
    }


    public static void main(String args[]) {
        try {

            // CouponCallService couponCallService = new CouponCallService();
            // // //Logger.debug(new CouponCallService().ReceiveCouponInfo(288484,1113,new
            // BigDecimal(100)));
            // //
            // // CouponBatchBean couponBatchBean = couponCallService.setCouponBatchBean(1,"1",2,1);
            // // String batchCode =
            // couponCallService.createSellerCoupon(couponBatchBean).getBatchCode();
            //
            // // couponCallService.ReceiveCouponInfo("1604156903", 1113);
            //
            // // couponCallService.ReceiveCouponInfo(1113);
            //
            // CreateCouponBean createCouponBean =
            // couponCallService.setDefaultCreatePlatformCouponBean();
            // CreateCouponCall createCouponCall =
            // couponCallService.createPlatformCoupon(createCouponBean);
            //
             int userId = Integer.valueOf(EnvSetup.getData("jCouponSellerId"));
            // couponCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);

            // Logger.debug(batchCode);

            // int sellerId = 288484;
            // int userId = 1113;
            // BigDecimal couponValue = new BigDecimal(10);
            // BigDecimal orderValue = new BigDecimal(100);
            // // String couponCode = new CouponCallService().receiveCouponInfo(sellerId, userId,
            // // couponValue);
            //
            // new CouponCallService().validCouponInfo("164617611720238699XCOdT3iO", orderValue,
            // userId, sellerId, null);
            //int userId = 20323545;
            // UserCouponListCall userCouponListCall = new
            // CouponCallService().getUserCouponList(userId, 1, 100, 1);
            //
            // UserCouponListData result = userCouponListCall.getUserCouponListData();
            //
            // List<MyCouponInfoBean> couponList = result.getUserCouponList();
            //
            // for (int i = 0; i < couponList.size(); i++) {
            // MyCouponInfoBean MyCouponInfoBean = couponList.get(0);
            // System.out.println(MyCouponInfoBean.getCouponCode());
            // }
/**创建商家券
            CouponCallService couponCallService = new CouponCallService();

            CouponBatchBean bean = couponCallService.setDefaultCouponBatchBean();
            // bean.getCouponValues().get(0).setOrderValue(orderValue);
            //bean.setSellerId(20227136);
            bean.setSellerId(512005);
           bean.setMaxSendNum(999999999);
            bean.setMaxSendNum(999999999);
            bean.setUserMaxReceiveNum(999999999);
            bean.setCouponName("StressTest_20160808_09");
             bean.getCouponValues().get(0).setOrderValue(new BigDecimal(100));
             bean.getCouponValues().get(0).setCouponValue(new BigDecimal(10));
             
            CreateSellerCouponCall createSellerCouponCall = couponCallService.createSellerCoupon(bean);

//            String couponCode = couponCallService.receiveCouponInfo(createSellerCouponCall.getBatchCode(), userId);
            System.out.println(createSellerCouponCall.getBatchCode());
 
            couponCallService.receiveCouponInfo(createSellerCouponCall.getBatchCode(), 1223344);
            
            */
 /**
  * 创建平台券*/
  
//            CouponCallService couponCallService = new CouponCallService();
//            CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformLinkCreateCouponBean() ;   
//            createCouponBean.setCouponNum(999999999);
//            createCouponBean.setPerCouponMaxUseCount(1);
//            createCouponBean.setPerUserReceiveMaxCount(999999999);
//            
//            Date dNow = new Date(); // 当前时间
//            Date dBefore = new Date();
//            Date dAfter = new Date();
//
//            Calendar calendar1 = Calendar.getInstance(); // 得到日历
//            calendar1.setTime(dNow);// 把当前时间赋给日历
//            calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
//            dBefore = calendar1.getTime(); // 得到前一天的时间
//
//            Calendar calendar2 = Calendar.getInstance(); // 得到日历
//            calendar2.setTime(dNow);// 把当前时间赋给日历
//            calendar2.add(Calendar.DAY_OF_MONTH, 720); // 设置为后十天
//            dAfter = calendar2.getTime(); // 得到前一天的时间
//
//            createCouponBean.setValidStart(dBefore);
//            createCouponBean.setValidEnd(dAfter);
//            
//            CouponValue couponValue = new CouponValue();
//            couponValue.setOrderMinAmount(new BigDecimal(61));
//            couponValue.setDeductionMaxAmount(new BigDecimal(60));
//
//            List<CouponValue> couponValues = new ArrayList<CouponValue>();
//            couponValues.add(couponValue);
//
//            createCouponBean.setCouponValues(couponValues);
//            
//            CreateCouponCall createCouponCall =couponCallService.createPlatformCoupon(createCouponBean);
//            System.out.println(createCouponCall.getBatchCode());
            
            // 领取
            CouponServiceCallServiceV2 couponCallService = new CouponServiceCallServiceV2();
            
//            for(int i=0;i<20;i++)
//            {
////            	System.out.println(couponCallService.receiveCouponInfo("1609086921", 20324128));
//            	System.out.println(couponCallService.receiveCouponInfo("1609233563", 20568095 ));
//            }
            
//            couponCallService.receiveCouponInfo(288484,20238699,new BigDecimal(1));
            
//            String batchCodes[] = {"1603300335","1603300336","1603300337","1603300338","1603300339","1603300340","1604150720","1611145987","1611145990","1611145993","1611146007","1611187894","1611187896","1611187899","1611187933","1611187952","1611187953","1611187954","1611187955","1611187957"};
//            
//            for (String batchCode : batchCodes) {
//            	 couponCallService.inValidCoupon(batchCode);
//                 
//			}
//            System.out.println(couponCallService.receiveCouponInfo(20238699, 11111, new BigDecimal(1)));
            System.out.println(couponCallService.receiveCouponInfo("1702228241",userId));
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
